#!/usr/bin/python
#-*- coding: utf-8 -*-

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.arrays import vbo
from numpy import array
from collections import namedtuple
import sys
import math

Point = namedtuple('POINT', ['x','y'], verbose=False)

p1 = Point(-6,-2) # 1
p2 = Point(-5,0) # 2
p3 = Point(-4,-2) # 3 
p4 = Point(-3,0) # 4
p5 = Point(-2,-2) # 5
p6 = Point(-1,0) # 6
p7 = Point(0,-2) # 7
p8 = Point(1,0) # 8
p9 = Point(0,2) # 9
p10 = Point(2,2) # 10
p11 = Point(3,0)# 11
p12 = Point(4,2) # 12
p13 = Point(5,0)# 13
p14 = Point(6,2) # 14

##points = [1,2,3,4,5,4,6,7,6,8,9,10,9,11,12,13]
points = [p1,p2,p3,
          p2,p4,p3,
          p3,p4,p5,
          p4,p6,p5,
          p5,p6,p7,
          p6,p8,p7,
          p6,p9,p8,
          p9,p10,p8,
          p8,p10,p11,
          p10,p12,p11,
          p11,p12,p13,
          p12,p14,p13
          ]

vbo = vbo.VBO(array(points, 'f'))

WIDTH, HEIGHT = 500, 500


def display():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(0., 0., 1.)
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
    
    # Render Vertexbuffer Object
    vbo.bind()
    
    # Pointer auf vbo setzen
    glVertexPointerf(vbo)
    glEnableClientState(GL_VERTEX_ARRAY)
    glDrawArrays(GL_TRIANGLES, 0, 36)
    vbo.unbind()
    glDisableClientState(GL_VERTEX_ARRAY)
    glFlush()
    
    

def initGL(w, h):
    glClearColor(1,1,1,1)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(-7., 7., -7., 7.)
    glMatrixMode(GL_MODELVIEW)
    
    
def main():
    
    # GLUT initialisieren
    glutInit(sys.argv) # kommandozeilenparameter uebergeben, glut library init
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) # Displaymodus fuer das Window
    glutInitWindowSize(WIDTH,HEIGHT)
    glutCreateWindow("Simple OpenGL Window")

    # Callback-Funktionen registrieren
    glutDisplayFunc(display)
    
   # Hack for Mac OS X
#   cwd = os.getcwd()
#   glutInit(sys.argv)
#   os.chdir(cwd)

    # OpenGL Kontext initialisieren
    initGL(WIDTH, HEIGHT)

    # Start GLUT Mainloop
    glutMainLoop()
    
    
    
if __name__ == '__main__':
    main()
